package org.zfes.snowier.cloud.zbss.loan.base.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.data.Record;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.loan.base.service.IBaseDatadictionaryService;
import org.zfes.snowier.cloud.zbss.loan.base.dao.BaseDatadictionaryMapper;
import org.zfes.snowier.cloud.zbss.loan.base.model.BaseDatadictionary;
import java.util.stream.Stream;
@Service
public class BaseDatadictionaryServiceImpl implements IBaseDatadictionaryService{
 	@Autowired
	private BaseDatadictionaryMapper baseDatadictionaryMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createBaseDatadictionary(BaseDatadictionary baseDatadictionary) {
		ZBeanUtil.setCreateInfo(baseDatadictionary);
		ZBeanUtil.validateBean(baseDatadictionary);
		baseDatadictionaryMapper.insertSelective(baseDatadictionary);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<BaseDatadictionary> loadBaseDatadictionaryById(Long id) {
		if(id!=null){
			return Optional.ofNullable(baseDatadictionaryMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateBaseDatadictionary(BaseDatadictionary baseDatadictionary) {
		ZBeanUtil.setUpdatedInfo(baseDatadictionary);
		ZBeanUtil.validateBean(baseDatadictionary);
		baseDatadictionaryMapper.updateByPrimaryKeySelective(baseDatadictionary);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteBaseDatadictionary(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		baseDatadictionaryMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setBaseDatadictionaryStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		baseDatadictionaryMapper.updateStatus( id,  status) ;
	}

	@Override
	public List<Record> loadBaseDatadictionaryForCode(String code) {
		ZAssert.notNull(code, "参数错误");
		return baseDatadictionaryMapper.loadBaseDatadictionaryForCode(code);
	}

	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadBaseDatadictionaryDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(baseDatadictionaryMapper.selectListCount(pm), baseDatadictionaryMapper.selectMapListPage(pm));
		return ds;
	}
	
}